//
//  RequestURL.m
//  LO_MUSIC
//
//  Created by lanou3g on 16/2/25.
//  Copyright © 2016年 杨少锋. All rights reserved.
//

#define kHomeUrl @"http://project.lanou3g.com/teacher/UIAPI/MusicInfoList.plist"

#import "RequestURL.h"

static RequestURL *requesturl = nil;

@implementation RequestURL

//- (void)requestMusiclistWithSuccess:(void (^)(id))success faile:(void (^)(NSError *))faile{
//    
//    
//}


+(instancetype)shareRequestUrl{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        requesturl = [[RequestURL alloc]init];
    });
    
    return requesturl;
}

//音乐首页的请求
- (void)requestMusiclistWithSuccess:(successBlock)success faile:(faileBlock)faile{
    //开辟子线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //请求数据
        NSArray *arr = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:kHomeUrl]];
        
        //如果请求到数据为空
        if (arr.count == 0) {
            
            NSError *err = [NSError errorWithDomain:@"请求出错" code:0 userInfo:nil];
            
            //回到主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                
                faile(err);
                
                return ;
            });
        }
        //创建一个数组盛放模型对象
        self.arrayReqesut = [NSMutableArray array];
        
        //解析
        
        for (NSDictionary *dic in arr) {
            MusicModel * model = [[MusicModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrayReqesut addObject:model];
        }
        
        //判断block 是否存在
        if (success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                
                success(self.arrayReqesut);
            });
            
        }
        
        
        
    });
 
}
@end

@implementation MusicModel

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
    
}

@end


